﻿Imports BUS
Imports DAO
Imports DTO
Imports System.Linq

Public Class LapDSLop

    Private Sub btn_Thoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Thoat.Click
        Close()
    End Sub

    Private Sub LapDSLop_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim khoi As KhoiBUS = New KhoiBUS()
        cbx_khoi.DisplayMember = "TenKhoi"
        cbx_khoi.ValueMember = "MaK"

        cbx_khoi.DataSource = khoi.getLevel_BUS()

        'Dim temp As Integer
        'Dim k As KhoiDTO = cbx_khoi.SelectedValue
        'temp = k.MaKhoi

        'Dim stt As Integer = 0
        'For Each row As DataGridViewRow In dgv_listst.Rows
        '    row.Cells(0).Value = stt + 1
        '    stt = stt + 1
        'Next
    End Sub

    Private Sub cbx_khoi_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbx_khoi.SelectedIndexChanged
        Dim temp As Integer
        Dim k As KhoiDTO = cbx_khoi.SelectedValue
        temp = k.MaKhoi

        Dim lop As LopBUS = New LopBUS()
        cbx_lop.DisplayMember = "TenLop"
        cbx_lop.ValueMember = "MaL"

        cbx_lop.DataSource = lop.getClass_BUS(temp)

        Dim st As HocSinhBUS = New HocSinhBUS()

        dgv_listst.DataSource = st.getStudent_BUS(temp)
        Dim stt As Integer = 0
        For Each row As DataGridViewRow In dgv_listst.Rows
            row.Cells(0).Value = stt + 1
            stt = stt + 1
        Next

        lbl_error.Text = ""
    End Sub

    Private Sub cbx_lop_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbx_lop.SelectedIndexChanged
        Dim lop As LopDTO = cbx_lop.SelectedValue
        txt_siso.Text = lop.SiSo

        lbl_error.Text = ""
    End Sub

    Private Sub btn_them_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_them.Click
        If Me.dgv_listst.RowCount = 0 Then
            lbl_error.Text = "Không có dữ liệu để thêm"
        Else
            Dim pfDto As HoSoHSDTO = New HoSoHSDTO()
            'pfDto.MaHoSo = "1"
            pfDto.MaHS = Me.dgv_listst.Item(1, dgv_listst.CurrentRow.Index).Value.ToString()

            Dim l As LopDTO = cbx_lop.SelectedValue
            pfDto.MaLop = l.MaLop

            pfDto.TinhTrang = "1"

            Dim lop As LopDTO = cbx_lop.SelectedValue
            If lop.SiSo < 40 Then
                Dim lopBus As LopBUS = New LopBUS()
                lopBus.InsertNumberofST_BUS(l.MaLop)

                Dim pfBus As HoSoHSBUS = New HoSoHSBUS()
                pfBus.addProfile_BUS(pfDto)
                lbl_error.Text = "Thêm học sinh thành công"
                txt_siso.Text = txt_siso.Text + 1
                'remove row
                dgv_listst.Rows.RemoveAt(dgv_listst.CurrentRow.Index)

            Else
                lbl_error.Text = "Vượt quá số lượng học sinh quy định trong 1 lớp"
            End If
        End If
    End Sub

    Private Sub btn_xoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_xoa.Click
        If Me.dgv_listst.RowCount = 0 Then
            lbl_error.Text = "Không có dữ liệu để xóa"
        Else
            Dim stDto As HocSinhDTO = New HocSinhDTO()
            stDto.MaHS = Me.dgv_listst.Item(1, dgv_listst.CurrentRow.Index).Value.ToString()

            Dim hsBus As HocSinhBUS = New HocSinhBUS()
            hsBus.deleteStudent_BUS(stDto.MaHS)

            dgv_listst.Rows.RemoveAt(dgv_listst.CurrentRow.Index)
            lbl_error.Text = "Xóa thành công"
        End If
    End Sub

End Class